package com.shadow.baiqun.delegate.receive.sendorder;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.shadow.baiqun.R;
import com.shadow.baiqun.R2;
import com.shadow.baiqun.config.ServiceUrl;
import com.shadow.core.delegate.RxShadowDelegate;
import com.shadow.core.net.callback.IError;
import com.shadow.core.net.callback.ISuccess;
import com.shadow.core.net.rx.RxRestClient;

import butterknife.BindView;

/**
 * 查看送货单
 */
public class SendOrderDelegate extends RxShadowDelegate {
    private static final RequestOptions REQUEST_OPTIONS =
            new RequestOptions().centerInside()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .dontAnimate();

    private static final String P_GID = "gid";

    public static SendOrderDelegate create(int pgid) {
        final SendOrderDelegate contentDelegate = new SendOrderDelegate();
        final Bundle bundle = new Bundle();
        bundle.putInt(P_GID, pgid);
        contentDelegate.setArguments(bundle);
        return contentDelegate;
    }

    @BindView(R2.id.image)
    ImageView mIv;
    @BindView(R2.id.toolbar)
    Toolbar mToolbar;
    private String mUrl;
    private int mGid;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mGid = bundle.getInt(P_GID);
        }
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_send_order_image;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    pop();
                }
            });
            mToolbar.setTitle("查看送货单");
        }
        requestUrl();
    }

    private void requestUrl() {
        mDisposables.add(RxRestClient.builder()
                .loader(getContext())
                .url("WMS/backstage/collectgoods/getserverpic")
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        String url = new UrlDataConverter().convert(response);
                        Glide.with(getContext())
                                .load(ServiceUrl.BASE_RELEASE_URL +
                                        "WMS/backstage/collectgoods/" + url)
                                .apply(REQUEST_OPTIONS)
                                .into(mIv);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {

                    }
                })
                .params("pgid", mGid)
                .build()
                .unRestGet());
    }
}
